package com.xuelang.pipeline.entry.resource;

import com.xuelang.pipeline.app.resource.SpaceService;
import com.xuelang.pipeline.client.common.constant.Constant;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.ApiResult;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.resource.ISpaceService;
import com.xuelang.pipeline.client.resource.dto.SpaceDTO;
import com.xuelang.pipeline.client.resource.dto.SpaceQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 交付空间服务web控制类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Slf4j
@RestController
@RequestMapping("/space")
public class SpaceController implements ISpaceService {

    @Autowired
    private SpaceService spaceService;

    @Override
    public ApiResult<PageRecord<SpaceDTO>> pageSpaceList(PageParam<SpaceQuery> dtoPageParam) {
        return ApiResult.success(spaceService.pageList(dtoPageParam));
    }

    @Override
    public ApiResult<List<SpaceDTO>> conditionalSpaceList(HttpServletRequest request, SpaceQuery spaceQuery) {
        String projectKey = request.getHeader(Constant.PROJECT_CODE_HEADER_KEY);
        spaceQuery.setProjectCode(projectKey);
        return ApiResult.success(spaceService.list(spaceQuery));
    }

    @Override
    public ApiResult<Boolean> addSpace(SpaceDTO spaceDTO) {
        return ApiResult.success(spaceService.add(spaceDTO));
    }

    @Override
    public ApiResult<Boolean> updateSpace(SpaceDTO spaceDTO) {
        return ApiResult.success(spaceService.update(spaceDTO));
    }

    @Override
    public ApiResult<Boolean> deleteSpace(String uuid) {
        return ApiResult.success(spaceService.delete(uuid));
    }

    @Override
    public ApiResult<SpaceDTO> querySpace(String uuid) {
        return ApiResult.success(spaceService.query(uuid));
    }

    @Override
    public ApiResult<Boolean> existsEnvironmentLimit(String spaceUuid) {
        return ApiResult.success(spaceService.existsEnvironmentBySpaceUuid(spaceUuid));
    }
}